package com.atguigu.guli.service.cms.controller.admin;


import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.cms.entity.Ad;
import com.atguigu.guli.service.cms.entity.AdType;
import com.atguigu.guli.service.cms.entity.vo.AdVo;
import com.atguigu.guli.service.cms.service.AdService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 广告推荐 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2021-08-09
 */

@Api(tags = "广告管理")
@RestController
@RequestMapping("/admin/cms/ad")
@Slf4j
public class AdminAdController {

    @Autowired
    AdService adService;


    @ApiOperation("推荐分页列表")
    @GetMapping("list/{pageNum}/{pageSize}")
    public R pageList(@PathVariable Integer pageNum,@PathVariable Integer pageSize){
        Page<AdVo> page = new Page<>(pageNum,pageSize);
        adService.selectPage(page);
        return R.ok().data("page",page);
    }

    @ApiOperation("新增广告")
    @PostMapping("save")
    public R save(@RequestBody Ad ad){
        boolean b = adService.save(ad);
        if (b){
            return R.ok().message("新增成功");
        }else {
            return R.error().message("新增失败");
        }
    }

    @ApiOperation("根据id查询广告")
    @GetMapping("getById/{id}")
    public R getById(@PathVariable String id){
        Ad ad = adService.getById(id);
        return R.ok().data("item",ad);
    }

    @ApiOperation("更新广告")
    @PutMapping("update")
    public R update(@RequestBody Ad ad){
        boolean b = adService.updateById(ad);
        if (b){
            return R.ok().message("修改成功");
        }else{
            return R.error().message("更新失败");
        }
    }

    @ApiOperation("根据id删除广告")
    @DeleteMapping("remove/{id}")
    public R remove(@PathVariable String id){

        //删除图片
        //adService.removeAdImageById(id);

        //删除视频
        boolean b = adService.removeById(id);
        if (b){
            return R.ok().message("删除成功");
        }else{
            return R.error().message("数据不存在");
        }
    }


}

